package com.codejie.pms.controller;


import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
@Controller
//@RequestMapping("/file/")
public class FileController {

    //文件上传
    @RequestMapping("/file/upload")
    public String upload(@RequestParam("uploadFile") MultipartFile uploadFile){
        //获取原始名字
        String fileName = uploadFile.getOriginalFilename();
        //获取后缀名
        //文件保存路径
        String filePath = "D:/uploadfile/";
        //文件重名名
        fileName = filePath+fileName;
        //文件对象
        File dest = new File(fileName);
        //判断路径是否存在，如果不存在则创建
        if (!dest.getParentFile().exists()){
            dest.getParentFile().mkdirs();
        }
        try {
            uploadFile.transferTo(dest);
            return "/my_mx";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "/false";
    }

    //文件下载
    @RequestMapping(value = "/downloadhq/{projectName}", method = RequestMethod.GET)
    public ResponseEntity<InputStreamResource> dowenloadhq(@PathVariable("projectName")String projectName) throws IOException {

        String filename = projectName+".doc";
        String filePath ="D:\\"+projectName+"\\后期文件\\"+filename;
        FileSystemResource file=new FileSystemResource(filePath);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control","no-cache,no-store,must-revalidate");
        headers.add("Content-Disposition",String.format("attachment;filename=\"%s\"",file.getFilename()));
        headers.add("Pragma","np-cache");
        headers.add("Expires","0");

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("applicat/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }

    //文件下载
    @RequestMapping(value = "/downloadxr/{projectName}", method = RequestMethod.GET)
    public ResponseEntity<InputStreamResource> dowenloadxr(@PathVariable("projectName")String projectName) throws IOException {

        String filename = projectName+".doc";
        String filePath ="D:\\"+projectName+"\\渲染文件\\"+filename;
        FileSystemResource file=new FileSystemResource(filePath);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control","no-cache,no-store,must-revalidate");
        headers.add("Content-Disposition",String.format("attachment;filename=\"%s\"",file.getFilename()));
        headers.add("Pragma","np-cache");
        headers.add("Expires","0");

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("applicat/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }

    //文件下载
    @RequestMapping(value = "/downloadmx/{projectName}", method = RequestMethod.GET)
    public ResponseEntity<InputStreamResource> dowenloadmx(@PathVariable("projectName")String projectName) throws IOException {

        String filename = projectName+".doc";
        String filePath ="D:\\"+projectName+"\\模型文件\\"+filename;
        FileSystemResource file=new FileSystemResource(filePath);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control","no-cache,no-store,must-revalidate");
        headers.add("Content-Disposition",String.format("attachment;filename=\"%s\"",file.getFilename()));
        headers.add("Pragma","np-cache");
        headers.add("Expires","0");

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("applicat/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }
}
